<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header-wrapped">
        <div class="header-content">
          <h3>通用后台管理系统</h3>
          <span class="welcome">欢迎您的登陆</span>
        </div>
      </el-header>
      <el-main>
        <div class="login-wrapped">
          <el-card class="box-card">
            <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick" :stretch="true">
              <el-tab-pane label="登录" name="first">
                <el-form label-width="auto" class="login-form">
                  <el-form-item label="账号">
                    <el-input v-model="loginData.account" placeholder="请输入账号" />
                  </el-form-item>
                  <el-form-item label="密码">
                    <el-input v-model="loginData.password" type="password" placeholder="请输入密码" />
                  </el-form-item>

                  <div class="footer-login">
                    <div class="forget-passsword">
                      <span @click="showForgetDialog">忘记密码</span>
                    </div>
                    <el-button type="primary" @click="handleLogin">登录</el-button>
                    <div class="register">
                      还没有账号？
                      <span class="register-text">马上注册</span>
                    </div>
                  </div>
                </el-form>
              </el-tab-pane>
              <el-tab-pane label="注册" name="second">
                <el-form label-width="auto" class="login-form">
                  <el-form-item label="账号">
                    <el-input v-model="registerData.account" placeholder="账号长度6-12位" />
                  </el-form-item>
                  <el-form-item label="密码">
                    <el-input v-model="registerData.password" type="password" placeholder="密码长度6-12位含数字和字母" />
                  </el-form-item>

                  <el-form-item label="确认密码">
                    <el-input v-model="registerData.confirmPassword" type="password" placeholder="请再次输入密码" />
                  </el-form-item>
                  <div class="footer-login">
                    <el-button type="primary" @click="handleRegister">注册</el-button>
                  </div>
                </el-form>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </div>
      </el-main>
      <el-footer class="footer-wrapped">
        <div class="footer-content">
          <div class="title"><span>test1</span> | <span>test2</span> | <span>test3 </span></div>
        </div>
      </el-footer>
    </el-container>
  </div>

  <ForgetPassword ref="forgetPassword"></ForgetPassword>
</template>

<script lang="ts" setup>
import { ElMessage, type TabsPaneContext } from 'element-plus'
import { reactive, ref } from 'vue'
import ForgetPassword from './components/ForgetPassword.vue'
import { login, register } from '@/api/login.js'
import router from '@/router'

const activeName = ref('first')
const forgetPassword = ref()
const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(tab, event)
}

interface LoginData {
  account: string
  password: string
  confirmPassword?: string
}

const loginData: LoginData = reactive({
  account: '',
  password: '',
})

const registerData: LoginData = reactive({
  account: '',
  password: '',
  confirmPassword: '',
})

async function handleLogin() {
  const res = await login(loginData)
  if (res.data.code == 0) {
    ElMessage({ message: '登录成功', type: 'success' })
    localStorage.setItem('token', res.data.token)
    router.push('/home')
  } else {
    ElMessage.error(res.data.msg ?? '登录失败, 请检查账号密码是否正确')
  }
  console.log(res.data)
}

function showForgetDialog() {
  forgetPassword.value.openForgetDailog()
}

const handleRegister = async () => {
  if (registerData.password !== registerData.confirmPassword) {
    ElMessage.error('密码不一致')
    return
  }
  const res = await register(registerData)
  console.log(res)
  if (res.data.code == 0 || res.data.msg == '账号注册成功') {
    ElMessage({ message: '注册成功', type: 'success' })
    activeName.value = 'first'
  } else {
    ElMessage.error(res.data.msg ?? '注册失败, 请检查账号密码是否正确')
  }
}
</script>

<style lang="scss" scoped>
.header-wrapped {
  .header-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .welcome {
      font-size: 13px;
    }
  }
}

.el-main {
  background-image: url('@/assets/bg-login.jpeg');
  height: 600px;
  .login-wrapped {
    width: 1200px;
    height: 600px;
    margin: 0 auto;

    .box-card {
      width: 350px;
      height: 375px;
      float: right;
      position: relative;
      top: 14%;

      .login-form {
        .footer-login {
          display: flex;
          flex-direction: column;

          .forget-passsword {
            color: #409eff;
            display: flex;
            justify-content: flex-end;
            font-size: 12px;
            margin: 10px 0;
            cursor: pointer;
          }

          :deep(.el-button) {
            width: 100%;
            height: 45px;
            font-size: 17px;
          }

          .register {
            font-size: 12px;
            margin: 12px 0;
            display: flex;
            justify-content: center;

            .register-text {
              font-size: 12px;
              color: #409eff;
              cursor: pointer;
            }
          }
        }
      }
    }
  }
}

.footer-wrapped {
  margin-top: 10px;
  .footer-content {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;

    .title {
      color: #000;
    }
    span {
      color: #666;
      font-size: 12px;
    }
  }
}

.el-form {
  margin-top: 30px;
}

:deep(.el-tabs__item) {
  font-size: 18px;
  color: #333;
}

:deep(.el-input__inner) {
  height: 40px;
}

:deep(.el-form-item__label) {
  line-height: 40px;
  height: 40px;
}
</style>
